<template>
  <div>
    <header class="app-header">
      <div class="header-content">
        <h1 class="app-title">数据库测试应用</h1>
        <nav class="app-nav" v-if="isLoggedIn">
          <router-link to="/" class="nav-link" :class="{ active: $route.name === 'Home' }">首页</router-link>
          <router-link to="/sections" class="nav-link" :class="{ active: $route.name === 'Sections' || $route.name === 'Section' }">板块</router-link>
          <router-link to="/profile" class="nav-link" :class="{ active: $route.name === 'UserProfile' }">个人中心</router-link>
        </nav>
        <nav class="app-nav" v-else>
          <router-link to="/" class="nav-link" :class="{ active: $route.name === 'Home' }">首页</router-link>
          <router-link to="/sections" class="nav-link" :class="{ active: $route.name === 'Sections' || $route.name === 'Section' }">板块</router-link>
        </nav>
      </div>
    </header>
    
    <main class="app-main">
      <router-view />
    </main>
    
    <!-- 登录和注册弹窗 -->
    <LoginModal v-if="showLoginModal" @login-success="handleLoginSuccess" ref="loginModal" />
    <RegisterModal v-if="showRegisterModal" ref="registerModal" />
  </div>
</template>

<script>
import LoginModal from './components/LoginModal.vue'
import RegisterModal from './components/RegisterModal.vue'

export default {
  name: 'App',
  components: {
    LoginModal,
    RegisterModal
  },
  data() {
    return {
      showLoginModal: false,
      showRegisterModal: false,
      isLoggedIn: false
    }
  },
  mounted() {
    // 检查用户登录状态
    this.checkLoginStatus();
    
    // 监听事件总线事件
    this.$eventBus.on('open-login', () => {
      this.showLoginModal = true;
      // 使用 nextTick 确保 DOM 更新后再尝试访问子组件
      this.$nextTick(() => {
        if (this.$refs.loginModal) {
          this.$refs.loginModal.open();
        }
      });
    });

    this.$eventBus.on('open-register', () => {
      this.showRegisterModal = true;
      // 使用 nextTick 确保 DOM 更新后再尝试访问子组件
      this.$nextTick(() => {
        if (this.$refs.registerModal) {
          this.$refs.registerModal.open();
        }
      });
    });
    
    // 监听登出事件
    this.$eventBus.on('user-logout', () => {
      this.isLoggedIn = false;
    });
    
    // 监听路由变化，更新登录状态
    this.$router.afterEach(() => {
      this.checkLoginStatus();
    });
  },
  methods: {
    checkLoginStatus() {
      const user = localStorage.getItem('user');
      this.isLoggedIn = !!user;
    },
    handleLoginSuccess(data) {
      // 登录成功后更新登录状态
      this.isLoggedIn = true;
      // 可以在这里处理登录成功后的逻辑
      console.log('登录成功:', data);
    }
  }
}
</script>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  background-color: #f5f7fa;
  color: #333;
  line-height: 1.6;
}

.app-header {
  background: linear-gradient(135deg, #007bff, #0056b3);
  color: white;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 60px;
}

.app-title {
  font-size: 1.5rem;
  font-weight: 600;
}

.app-nav {
  display: flex;
  gap: 20px;
}

.nav-link {
  color: rgba(255, 255, 255, 0.85);
  text-decoration: none;
  font-weight: 500;
  padding: 5px 10px;
  border-radius: 4px;
  transition: all 0.3s;
}

.nav-link:hover {
  color: white;
  background-color: rgba(255, 255, 255, 0.1);
}

.nav-link.active {
  color: white;
  background-color: rgba(255, 255, 255, 0.2);
}

.app-main {
  max-width: 1200px;
  margin: 0 auto;
  padding: 30px 20px;
  min-height: calc(100vh - 60px);
}
</style>